package com.settlement.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.common.annotation.PreventDuplicateSubmit;
import com.settlement.system.common.result.PageResult;
import com.settlement.system.common.result.Result;
import com.settlement.system.model.entity.BsOmsOrder;
import com.settlement.system.model.vo.BsOmsOrderVo;
import com.settlement.system.service.BsOmsOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.settlement.system.model.query.BsOmsOrderQuery;
import com.settlement.system.model.form.BsOmsOrderForm;
import org.springdoc.core.annotations.ParameterObject;
import io.swagger.v3.oas.annotations.Parameter;
import com.settlement.system.model.vo.BsOmsOrderImportVo;
import com.settlement.system.listener.easyexcel.BsOmsOrderListener;
import com.alibaba.excel.EasyExcel;

import com.alibaba.excel.EasyExcelFactory;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 * 百盛订单表 前端控制器
 * </p>
 *
 * @author huangrensen
 * @since 2024-09-04
 */
@RestController
@RequestMapping("/api/v1/bsOmsOrder")
@RequiredArgsConstructor
public class BsOmsOrderController {

    private final BsOmsOrderService bsOmsOrderService;

    @GetMapping("/page")
    public PageResult<BsOmsOrderVo> getPage(@ParameterObject BsOmsOrderQuery queryParams) {
        Page<BsOmsOrderVo> result = bsOmsOrderService.getPage(queryParams);
        return PageResult.success(result);
    }


    @Operation(summary = "保存百盛订单表", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping
    @PreventDuplicateSubmit
    @PreAuthorize("@ss.hasPerm('sys:bsOmsOrder:save')")
    public Result<?> saveBsOmsOrder(@RequestBody @Valid BsOmsOrderForm bsOmsOrderForm) {
        boolean result = bsOmsOrderService.saveBsOmsOrder(bsOmsOrderForm);
        return Result.judge(result);
    }

    @Operation(summary = "获取百盛订单表信息", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/{id}/form")
    public Result<BsOmsOrderForm> getBsOmsOrderFormData(@Parameter(description = "主键ID") @PathVariable Long id) {
        BsOmsOrderForm bsOmsOrder = bsOmsOrderService.getBsOmsOrderFormData(id);
        return Result.success(bsOmsOrder);
    }

    @Operation(summary = "删除百盛订单表", security = {@SecurityRequirement(name = "Authorization")})
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:bsOmsOrder:delete')")
    public Result<?> deleteBsOmsOrder(@Parameter(description = "ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        boolean result = bsOmsOrderService.deleteBsOmsOrder(ids);
        return Result.judge(result);
    }

    @Operation(summary = "百盛订单表导入", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("/bsOmsOrderImport")
    @PreAuthorize("@ss.hasPerm('sys:bsOmsOrder:import')")
    @PreventDuplicateSubmit
    public Result<?> bsOmsOrderImport(@RequestParam("file") MultipartFile file) throws Exception {
        BsOmsOrderListener listener = new BsOmsOrderListener();
        EasyExcelFactory.read(file.getInputStream(), BsOmsOrderImportVo.class, listener).sheet(0).doRead();
        List<BsOmsOrderImportVo> list = listener.getVos();
        if (list == null || list.size() == 0) {
            return Result.success();
        }
        bsOmsOrderService.importData(list);
        return Result.success();
    }

    @Operation(summary = "导出百盛订单表", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/_export")
    @PreAuthorize("@ss.hasPerm('sys:bsOmsOrder:export')")
    @PreventDuplicateSubmit
    public void export(@RequestParam("ids") String ids, HttpServletResponse response) throws IOException {
        String fileName = "百盛订单表.xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
        List<BsOmsOrderImportVo> bsOmsOrderList = bsOmsOrderService.findByIds(ids);
        EasyExcel.write(response.getOutputStream(), BsOmsOrderImportVo.class).sheet("百盛订单表").doWrite(bsOmsOrderList);
    }
}

